﻿using Andy.Web.SanPhamServiceReference;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Vdc.Grid;

namespace Andy.Web.admin
{
    public partial class ht_sanpham : System.Web.UI.Page
    {
        static int recodperpage_sp = 12;
        static int Pagesize_sp = 5;
        static string func_sp = "paging_sp";
        protected void Page_Load(object sender, EventArgs e)
        {

            int page = int.Parse("0" + Request.QueryString["page"]);
            if (page == 0) page = 1;
            if (!IsPostBack)
            {
                loadGid(page);
            }
        }
        public void loadGid(int page)
        {
            using (var spService = new SanPhamServiceReference.SanPhamServiceClient())
            {
                var lst = spService.DanhSachSanPham(0, page, recodperpage_sp, Pagesize_sp, null);
                if (lst != null)
                {
                    if (lst.lstSanPham != null)
                    {
                        grid1.DataSource = lst.lstSanPham;
                        grid1.DataBind();

                        ltlPhanTrang.Text = lst.PhanTrangSP;
                    }
                }
            }
        }

        protected void VdcButton1_Click(object sender, EventArgs e)
        {
            string selectedOrderIds = "";

            for (int i = 0; i < grid1.RowsInViewState.Count; i++)
            {
                GridDataControlFieldCell cell = grid1.RowsInViewState[i].Cells[0] as GridDataControlFieldCell;
                CheckBox chk = cell.FindControl("ChkID") as CheckBox;
                HiddenField hiddenField = cell.FindControl("ProductName") as HiddenField;
                if (chk.Checked.ToString() != chk.ToolTip)
                {
                    selectedOrderIds += hiddenField.Value + "-" + chk.Checked.ToString() + ",";
                }
            }
            List<string> c = selectedOrderIds.Split(',').ToList();
            List<string> item = null;
            List<SanPham> lst = new List<SanPham>();
            foreach (string d in c)
            {
                if (d != "")
                {
                    item = d.Split('-').ToList();
                    SanPham sp = new SanPham();
                    sp.Id = Convert.ToInt64(item[0]);
                    sp.CheckNoiBat = Convert.ToBoolean(item[1]);
                    lst.Add(sp);
                }

            }
            using (var spService = new SanPhamServiceReference.SanPhamServiceClient())
            {
                var resultCapNhat = spService.CapNhatSanPhamNoiBat(lst);
                if (resultCapNhat == null) resultCapNhat = "Thất bại";
                ScriptManager.RegisterStartupScript(Page, typeof(Page), "SymbolError", "alert('Cập nhật " + resultCapNhat + "')", true);
            }

        }

        protected void grid1_RowDataBound(object sender, GridRowEventArgs e)
        {
            if (e.Row.RowType == GridRowType.DataRow && grid1.RowsInViewState.Count > 0)
            {
                if (grid1.RowsInViewState.Count > e.Row.RowIndex)
                {
                    GridDataControlFieldCell cell = e.Row.Cells[0] as GridDataControlFieldCell;
                    CheckBox ckBox = cell.FindControl("ChkID") as CheckBox;
                    HiddenField hiddenField = cell.FindControl("ProductName") as HiddenField;

                    GridDataControlFieldCell cellInViewState = grid1.RowsInViewState[e.Row.RowIndex].Cells[0] as GridDataControlFieldCell;
                    CheckBox ckBoxInViewState = cellInViewState.FindControl("ChkID") as CheckBox;
                    HiddenField hiddenFieldInViewState = cellInViewState.FindControl("ProductName") as HiddenField;

                    ckBox.Checked = ckBoxInViewState.Checked;
                    hiddenField.Value = hiddenFieldInViewState.Value;
                }
            }
        }

    }
}